package org.airlink.persistence.criteria
{
	import flash.utils.getQualifiedClassName;
	
	import flex.persistence.EntityManager;
	import flex.persistence.criteria.CriteriaQuery;
	import flex.persistence.criteria.From;
	import flex.persistence.criteria.Join;
	import flex.persistence.criteria.JoinType;
	import flex.persistence.metamodel.Attribute;
	
	import org.airlink.persistence.Entity;
	import org.airlink.persistence.EntityManagerImpl;
	import org.airlink.persistence.metamodel.AttributeImpl;
	
	public class FromImpl extends PathImpl implements From
	{
		private var _joins:Array;
		private var _correlated:Boolean;
		
		
		public function join(attribute:String, joinType:String, criteriaQuery:CriteriaQuery):Join {
			var join:Join = new JoinImpl(new AttributeImpl(_cls, attribute, _em, criteriaQuery.aliasIndex), new JoinType(joinType));			
			if(_joins == null)
				_joins = new Array();
			_joins.push(join);			
			return join;
		}
		
		public function FromImpl(cls:Class = null, parent:PathImpl = null, attribute:Attribute = null,em:EntityManagerImpl = null, aliasIndex:uint = 0)
		{
			super(cls, parent, attribute, em, aliasIndex);
		}
		
		//TODO : implementer ou supprimer (inutile jusqu'alors)
		public function get correlationParent():From{
			return null; 
		}
		
		public function get joins():Array{
			return _joins;
		}
		
		public function isCorrelated():Boolean{
			return _correlated;
		}
		
		public override function toString():String{
			var entity:Entity = _em.introspector.loadMetadata(_cls);
			if(entity.tableName != null)
				return entity.tableName+" t"+super.aliasIndex;
			else
				
				return getClassName(super.clazz)+" t"+super.aliasIndex; 
		}
		
		private function getClassName(object:Object):String {
			if(object != null)
				return getQualifiedClassName(object).split("::").pop();			
			return null;
		}
	}
}